using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using Spacejock;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using yW5Lib;

namespace yWriter6
{
	public class clsPrintScene
	{
		public delegate void PrintProgressEventHandler(string ProgressString);

		[CompilerGenerated]
		private PrintProgressEventHandler PrintProgressEvent;

		private clsRTF RTFToText;

		private Collection mvarIDs;

		public event PrintProgressEventHandler PrintProgress
		{
			[CompilerGenerated]
			add
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Combine(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
			[CompilerGenerated]
			remove
			{
				PrintProgressEventHandler printProgressEventHandler = PrintProgressEvent;
				PrintProgressEventHandler printProgressEventHandler2;
				do
				{
					printProgressEventHandler2 = printProgressEventHandler;
					PrintProgressEventHandler value2 = (PrintProgressEventHandler)Delegate.Remove(printProgressEventHandler2, value);
					printProgressEventHandler = Interlocked.CompareExchange(ref PrintProgressEvent, value2, printProgressEventHandler2);
				}
				while ((object)printProgressEventHandler != printProgressEventHandler2);
			}
		}

		public clsPrintScene()
		{
			RTFToText = new clsRTF(ref clsShared.cLog, ref clsShared.cSettings);
			mvarIDs = new Collection();
		}

		public void ClearIDs()
		{
			mvarIDs.Clear();
		}

		public void AddID(int ID)
		{
			mvarIDs.Add(ID);
		}

		public bool PrintSceneToFile(clsProject cProject, string ExportFilename, int ScID)
		{
			bool result = false;
			try
			{
				clsScene cScene = cProject.get_SceneByID(ScID);
				clsReport Report = new clsReport(ref clsShared.cLog, ref clsShared.cSettings, "");
				LocalPrintSceneText(cProject, ref Report, ref cScene);
				clsShared.cFile.WriteTextFile(ExportFilename, Report.ReportContent());
				result = true;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public void PrintScenes(clsProject cProject, bool TextOnly)
		{
			clsReport Report = new clsReport(ref clsShared.cLog, ref clsShared.cSettings, "Selected Scenes in " + cProject.Title);
			clsShared.cSubs.BusyMouse();
			int num = 0;
			IEnumerator enumerator = default(IEnumerator);
			try
			{
				enumerator = mvarIDs.GetEnumerator();
				while (enumerator.MoveNext())
				{
					int num2 = Conversions.ToInteger(enumerator.Current);
					num++;
					if (TextOnly)
					{
						clsScene cScene = cProject.get_SceneByID(num2);
						LocalPrintSceneText(cProject, ref Report, ref cScene);
					}
					else
					{
						LocalPrintScene(cProject, num2, ref Report);
						if (num < mvarIDs.Count)
						{
							Report.AddText(Report.PageBreakCode);
						}
					}
				}
			}
			finally
			{
				if (enumerator is IDisposable)
				{
					(enumerator as IDisposable).Dispose();
				}
			}
			clsShared.cSubs.NormalMouse();
			Report.AddFooter(App.Title);
			Report.Show();
			PrintProgressEvent?.Invoke("Printing Complete");
			mvarIDs.Clear();
		}

		public void PrintScene(clsProject cProject, int ScID, bool TextOnly)
		{
			clsReport Report = new clsReport(ref clsShared.cLog, ref clsShared.cSettings, cProject.get_SceneByID(ScID).Title);
			clsShared.cSubs.BusyMouse();
			PrintScene(ref Report, cProject, ScID, TextOnly);
			clsShared.cSubs.NormalMouse();
			Report.AddFooter(App.Title);
			Report.Show();
			PrintProgressEvent?.Invoke("Printing Complete");
		}

		public void PrintScene(ref clsReport Report, clsProject cProject, int ScID, bool TextOnly)
		{
			if (TextOnly)
			{
				clsScene cScene = cProject.get_SceneByID(ScID);
				LocalPrintSceneText(cProject, ref Report, ref cScene);
			}
			else
			{
				LocalPrintScene(cProject, ScID, ref Report);
			}
		}

		private void LocalPrintScene(clsProject cProject, int ScID, ref clsReport Report)
		{
			//Discarded unreachable code: IL_0627, IL_063e, IL_0649, IL_0659, IL_07bb, IL_07bd, IL_07cd, IL_07ef
			int num2 = default(int);
			int num3 = default(int);
			try
			{
				int num = 1;
				clsScene cScene = cProject.get_SceneByID(ScID);
				num = 2;
				clsChapter clsChapter = cProject.get_ChapterByID(cScene.BelongsToChID);
				num = 3;
				PrintProgressEvent?.Invoke("Printing Scene ID " + Conversions.ToString(ScID));
				num = 4;
				Report.OpenTable();
				num = 5;
				Report.OpenRow();
				num = 6;
				Report.OpenCell();
				num = 7;
				Report.AddBR();
				num = 8;
				Report.AddText("<i><font color='gray'>");
				num = 9;
				Report.AddText("<hr>");
				num = 10;
				if (cScene.Unused | cProject.get_ChapterByID(cScene.BelongsToChID).Unused)
				{
					num = 11;
					Report.AddText("UNUSED Scene: " + Report.Bold(cScene.Title));
				}
				else
				{
					num = 13;
					Report.AddText("Title: " + Report.Bold(cScene.Title));
				}
				num = 14;
				Report.AddBR();
				num = 15;
				Report.AddText("Chapter: " + Report.Bold(clsChapter.Title) + ", Scene # " + Report.Bold(Conversions.ToString(clsChapter.SceneIndex(cScene.ID))));
				num = 16;
				Report.AddBR();
				num = 17;
				Report.AddText("Viewpoint: " + Report.Bold(cProject.get_CharByID(cScene.ViewPointID).Title));
				num = 18;
				int i;
				if (cScene.CharCount > 1)
				{
					num = 19;
					Report.AddText(".  Also Present: ");
					num = 20;
					int charCount = cScene.CharCount;
					for (i = 2; i <= charCount; i++)
					{
						num = 21;
						Report.AddText(cProject.get_CharByID(((clsSceneBase)cScene).get_CharID(i)).Title);
						num = 22;
						if (i < cScene.CharCount)
						{
							num = 23;
							Report.AddText(", ");
						}
						num = 24;
					}
				}
				num = 25;
				Report.AddBR();
				num = 26;
				if (cScene.LocationCount > 0)
				{
					num = 27;
					Report.AddText("Location(s): ");
					num = 28;
					int locationCount = cScene.LocationCount;
					for (i = 1; i <= locationCount; i++)
					{
						num = 29;
						Report.AddText(cProject.get_LocationByID(((clsSceneBase)cScene).get_LocationID(i)).Title);
						num = 30;
						if (i < cScene.LocationCount)
						{
							num = 31;
							Report.AddText(", ");
						}
						num = 32;
					}
					num = 33;
					Report.AddBR();
				}
				num = 34;
				if (cScene.ItemCount > 0)
				{
					num = 35;
					Report.AddText("Item(s): ");
					num = 36;
					int itemCount = cScene.ItemCount;
					for (i = 1; i <= itemCount; i++)
					{
						num = 37;
						Report.AddText(cProject.get_ItemByID(((clsSceneBase)cScene).get_ItemID(i)).Title);
						num = 38;
						if (i < cScene.ItemCount)
						{
							num = 39;
							Report.AddText(", ");
						}
						num = 40;
					}
					num = 41;
					Report.AddBR();
				}
				num = 42;
				i = 1;
				do
				{
					num = 43;
					Report.AddText(cProject.get_FieldTitle(i) + ": " + Strings.Format(((clsSceneBase)cScene).get_Field(i)));
					num = 44;
					if (i < 4)
					{
						num = 45;
						Report.AddText(", ");
					}
					num = 46;
					i++;
				}
				while (i <= 4);
				num = 47;
				Report.AddBR();
				num = 48;
				if (cScene.ReactionScene)
				{
					num = 49;
					if (Operators.CompareString(cScene.Goal, "", TextCompare: false) != 0)
					{
						num = 50;
						Report.AddText(clsShared.cLang.GetTranslation("Reaction: ") + Report.Bold(cScene.Goal));
						num = 51;
						Report.AddBR();
					}
					num = 52;
					if (Operators.CompareString(cScene.Conflict, "", TextCompare: false) != 0)
					{
						num = 53;
						Report.AddText(clsShared.cLang.GetTranslation("Dilemma: ") + Report.Bold(cScene.Conflict));
						num = 54;
						Report.AddBR();
					}
					num = 55;
					if (Operators.CompareString(cScene.Outcome, "", TextCompare: false) != 0)
					{
						num = 56;
						Report.AddText(clsShared.cLang.GetTranslation("Decision: ") + Report.Bold(cScene.Outcome));
						num = 57;
						Report.AddBR();
					}
				}
				else
				{
					num = 59;
					if (Operators.CompareString(cScene.Goal, "", TextCompare: false) != 0)
					{
						num = 60;
						Report.AddText(clsShared.cLang.GetTranslation("Goal: ") + Report.Bold(cScene.Goal));
						num = 61;
						Report.AddBR();
					}
					num = 62;
					if (Operators.CompareString(cScene.Conflict, "", TextCompare: false) != 0)
					{
						num = 63;
						Report.AddText(clsShared.cLang.GetTranslation("Conflict: ") + Report.Bold(cScene.Conflict));
						num = 64;
						Report.AddBR();
					}
					num = 65;
					if (Operators.CompareString(cScene.Outcome, "", TextCompare: false) != 0)
					{
						num = 66;
						Report.AddText(clsShared.cLang.GetTranslation("Outcome: ") + Report.Bold(cScene.Outcome));
						num = 67;
						Report.AddBR();
					}
				}
				num = 68;
				if (!cProject.V6File)
				{
					num = 69;
					Report.AddText("Scene Filename: " + Report.Bold(cScene.RTFFilename));
					num = 70;
					Report.AddBR();
				}
				num = 71;
				if (Operators.CompareString(cScene.DescText.Trim(), "", TextCompare: false) != 0)
				{
					num = 72;
					Report.AddBR();
					num = 73;
					Report.AddText("Desc: " + cScene.DescText);
					num = 74;
					Report.AddBR(2);
				}
				num = 75;
				Report.AddText("</i></font>");
				num = 76;
				Report.AddText("<hr><br />");
				num = 77;
				LocalPrintSceneText(cProject, ref Report, ref cScene);
				num = 78;
				Report.AddBR(2);
				num = 79;
				Report.CloseCell();
				num = 80;
				Report.CloseRow();
				num = 81;
				Report.CloseTable();
			}
			catch (Exception obj) when ((obj is Exception && num2 != 0) & (num3 == 0))
			{
				ProjectData.SetProjectError((Exception)obj);
				/*Error near IL_07ed: Could not find block for branch target IL_07bd*/;
			}
			if (num3 != 0)
			{
				ProjectData.ClearProjectError();
			}
		}

		private void LocalPrintSceneText(clsProject cProject, ref clsReport Report, ref clsScene cScene)
		{
			clsShared.cSettings.sGet("EditorFontName", "Segoe UI");
			clsShared.cSettings.sGet("EditorFontSize", 10f);
			string rTF = modSubs.RTFWithParsedVars(cProject, cScene, modVariables.eFinalExportType.Html);
			string BodyText = RTFToText.RtfToHTML(rTF, IgnoreDocFont: false);
			clsShared.cText.RemoveCRLFFromEnd(ref BodyText);
			BodyText = BodyText.Trim();
			Report.AddText(BodyText, WithCRLFConversion: false);
		}
	}
}
